package com.service.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.sys.RoleModulePermissionDao;
import com.model.sys.RoleModulePermissionModel;
import com.common.model.Global;
import com.model.sys.SysUser; 
import com.common.spring.SpringMvcUtils;

@Service
public class RoleModulePermissionService {
	static final Logger logger = LoggerFactory.getLogger(RoleModulePermissionService.class);

	@Autowired
	RoleModulePermissionDao roleModulePermissionDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		SysUser sysUser = SpringMvcUtils.getSysUser();
		if (!sysUser.getSuperadmin().equals(Global.YES + "")) {
			if (StringUtils.isNotNull(sysUser.getDatapermission())) {
				queryMap.put("deptIds", sysUser.getDatapermission().split(","));
			} else {
				queryMap.put("createId", sysUser.getUserid());
			}
		}
		List<RoleModulePermissionModel> list = roleModulePermissionDao.selectList(queryMap);
		int count = roleModulePermissionDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 * 
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String ROLEID,String MODULEID,String PERMISSION) {
		HttpResult httpResult = new HttpResult();
		RoleModulePermissionModel roleModulePermissionModel = roleModulePermissionDao.load(ROLEID,MODULEID,PERMISSION);
		httpResult.setData(roleModulePermissionModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult save(RoleModulePermissionModel roleModulePermissionModel) {
		if (roleModulePermissionModel == null) {
			return HttpResult.error("参数不能为空");
		}
		HttpResult httpResult = new HttpResult();
		int result = 0;
		if (!StringUtils.isNotNull(roleModulePermissionModel.getROLEID())) {
			roleModulePermissionModel.setROLEID(UUID.randomUUID().toString());
			result = roleModulePermissionDao.insert(roleModulePermissionModel);
		} else {
			result = roleModulePermissionDao.update(roleModulePermissionModel);
		}
		if (!StringUtils.isNotNull(roleModulePermissionModel.getMODULEID())) {
			roleModulePermissionModel.setMODULEID(UUID.randomUUID().toString());
			result = roleModulePermissionDao.insert(roleModulePermissionModel);
		} else {
			result = roleModulePermissionDao.update(roleModulePermissionModel);
		}
		if (!StringUtils.isNotNull(roleModulePermissionModel.getPERMISSION())) {
			roleModulePermissionModel.setPERMISSION(UUID.randomUUID().toString());
			result = roleModulePermissionDao.insert(roleModulePermissionModel);
		} else {
			result = roleModulePermissionDao.update(roleModulePermissionModel);
		}
//		if (result > 0) {
//			httpResult.setMsg(roleModulePermissionModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
//			httpResult.setData(roleModulePermissionModel);
//		} else {
//			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
//			httpResult.setMsg(roleModulePermissionModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
//		}
		return httpResult;
	}

	/**
	 * 提交
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult commit(RoleModulePermissionModel roleModulePermissionModel) {
		if (roleModulePermissionModel == null) {
			return HttpResult.error("参数不能为空");
		}
//		roleModulePermissionModel.setStatus(Global.YES+"");
		HttpResult httpResult = new HttpResult();
		int result = 0;
			result = roleModulePermissionDao.update(roleModulePermissionModel);
			result = roleModulePermissionDao.update(roleModulePermissionModel);
			result = roleModulePermissionDao.update(roleModulePermissionModel);
		if (result > 0) {
//			httpResult.setMsg(roleModulePermissionModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
//			httpResult.setData(roleModulePermissionModel);
		} else {
//			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
//			httpResult.setMsg(roleModulePermissionModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
		}
		return httpResult;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult delete(String ROLEID,String MODULEID,String PERMISSION) {
		if (!StringUtils.isNotNull(ROLEID)||!StringUtils.isNotNull(MODULEID)||!StringUtils.isNotNull(PERMISSION)) {
			return HttpResult.error("参数不能为空");
		}
		RoleModulePermissionModel model = roleModulePermissionDao.load(ROLEID,MODULEID,PERMISSION);
		if(model==null){
			return HttpResult.error("无数据！");
		}
//		if (!model.getStatus().equals(Global.NO + "")) {
//			return HttpResult.error("非待提交状态，不可删除！");
//		}		
		HttpResult httpResult = new HttpResult();
		int result = roleModulePermissionDao.delete(ROLEID,MODULEID,PERMISSION);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 撤回
	 *
	 * @param
	 * @return
	 */
	@Transactional
	public HttpResult back(String ROLEID,String MODULEID,String PERMISSION) {
		if (!StringUtils.isNotNull(ROLEID)||!StringUtils.isNotNull(MODULEID)||!StringUtils.isNotNull(PERMISSION)) {
			return HttpResult.error("参数不能为空");
		}
		RoleModulePermissionModel model = roleModulePermissionDao.load(ROLEID,MODULEID,PERMISSION);
		
		//todo	
		
		HttpResult httpResult = new HttpResult();
//		model.setStatus(Global.NO + "");
		int result = roleModulePermissionDao.update(model);
		if (result > 0) {
			httpResult.setMsg("撤回成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 * 
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<RoleModulePermissionModel> list = roleModulePermissionDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), RoleModulePermissionModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}
}
